package com.itheima.hmshopping.dubbo.apiImpl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.hmshopping.dubbo.api.SmsFlashPromotionProductRelationApi;
import com.itheima.hmshopping.dubbo.mapper.SmsFlashPromotionProductRelationMapper;
import com.itheima.hmshopping.entity.SmsFlashPromotionProductRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsFlashPromotionProductRelationImpl implements SmsFlashPromotionProductRelationApi {
    @Autowired
    private SmsFlashPromotionProductRelationMapper productRelationMapper;

    /**
     * 批量选择商品添加关联
     * @param list
     */
    @Override
    public void create(List<SmsFlashPromotionProductRelation> list) {
        for (SmsFlashPromotionProductRelation smsFlashPromotionProductRelation : list) {
            productRelationMapper.insert(smsFlashPromotionProductRelation);
        }
    }

    /**
     * 删除关联
     * @param id
     */
    @Override
    public void delete(Integer id) {
        productRelationMapper.deleteById(Convert.toLong(id));
    }

    /**
     * 查询关联
     * @param smsFlashPromotionProductRelation
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page list(SmsFlashPromotionProductRelation smsFlashPromotionProductRelation, Integer pageNum, Integer pageSize) {
        //构造条件
        LambdaQueryWrapper<SmsFlashPromotionProductRelation>wrapper=new LambdaQueryWrapper<>();
        if (smsFlashPromotionProductRelation != null) {
            wrapper.eq(smsFlashPromotionProductRelation.getFlashPromotionId() != null, SmsFlashPromotionProductRelation::getFlashPromotionId, smsFlashPromotionProductRelation.getFlashPromotionId())
                    .eq(smsFlashPromotionProductRelation.getFlashPromotionSessionId() != null, SmsFlashPromotionProductRelation::getFlashPromotionSessionId, smsFlashPromotionProductRelation.getFlashPromotionSessionId())
                    .orderByDesc(SmsFlashPromotionProductRelation::getSort);

        }
        Page page = new Page<>(pageNum, pageSize);
        Page selectPage = productRelationMapper.selectPage(page, wrapper);
        return selectPage;
    }

    /**
     * 修改关联信息
     * @param id
     * @param relation
     */
    @Override
    public void update(Long id, SmsFlashPromotionProductRelation relation) {
        productRelationMapper.updateById(relation);
    }

    /**
     * 获取场次详情
     * @param id
     * @return
     */
    @Override
    public SmsFlashPromotionProductRelation getOne(Long id) {
       return productRelationMapper.selectById(id);
    }


}
